#include "playscene.h"
#include <QDebug>
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include "mycoin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>
PlayScene::PlayScene(int levelNum)
{
    QString str=QString("您选择的是第%1关").arg(levelNum);
    qDebug()<<str;
    this->levelIndex=levelNum;
    setFixedSize(320, 600);//设置固定大小
    setWindowIcon(QIcon(":/res/01.png"));//设置图标
    setWindowTitle("CoinFlip");//设置标题
    QMenuBar* bar=menuBar();//创建菜单栏
    setMenuBar(bar);
    QMenu* startMenu=bar->addMenu("开始");//创建开始菜单
    QAction* quitAction=startMenu->addAction("退出");//创建退出菜单项
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    MyPushButton* backBtn=new MyPushButton(":/res/back.png");//返回按钮
    backBtn->setParent(this);
    backBtn->move(this->width()- backBtn->width(),this->height()-backBtn->height());
    connect(backBtn, &MyPushButton::clicked, [=]()
            {
                QTimer::singleShot(500, this, [=](){
                    emit this->chooseSceneBack();//返回主场景
                });//延时返回场景
            });
    QLabel* label=new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("宋体");
    font.setPointSize(20);
    QString str1=QString("Level:%1").arg(this->levelIndex);
    label->setFont(font);
    label->setText(str1);
    label->setGeometry(30,this->height()-50,120,50);//设计选择关卡按钮
    dataconfig config;
    for(int i=0;i<4;i++)
        {
        for(int j=0;j<4;j++)
        {
            this->gameArray[i][j]=config.mData[this->levelIndex][i][j];
        }
        }//初始化每个关卡的二维数组
    QLabel* winLabel=new QLabel;
        QPixmap tmpPix;
    tmpPix.load(":/res/succeed.png");
        winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
        winLabel->setParent(this);
    winLabel->move((this->width()-tmpPix.width())*0.5,-tmpPix.height());//插入胜利动画

    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            QPixmap pix=QPixmap(":/res/broadnode.png");
             QLabel* label=new QLabel;
            label->setGeometry(0,0,pix.width(),pix.height());
             label->setPixmap(pix);
             label->setParent(this);
             label->move(57+i*50,200+j*50);//显示金币背景图案
             QString str;
             if(this->gameArray[i][j]==1)
             {
                 str=":/res/01.png";
             }//显示金币
             else
             {
                 str=":/res/08.png";
             }//显示银币
             MyCoin* coin=new MyCoin(str);
             coin->setParent(this);
             coin->move(59+i*50,204+j*50);//创建金币
             coin->posX=i;
             coin->posY=j;
             coin->flag=this->gameArray[i][j]==1;//1正面 0反面
             coinBtn[i][j]=coin;//将金币放入到金币的二位数组中，以便后期维护
             connect(coin,&MyCoin::clicked,[=]()
                     {
                         for(int i=0;i<4;i++)
                         {
                             for (int j=0;j<4;j++)
                             {
                                 this->coinBtn[i][j]->isWin=true;
                             }
                         }//点击瞬间，禁用所有按钮
                         coin->changeFlag();
                         this->gameArray[i][j]=this->gameArray[i][j]==0?1:0;
                         QTimer::singleShot(300,this,[=](){
                             if(coin->posX+1<=3)
                             {
                                 coinBtn[coin->posX+1][coin->posY]->changeFlag();
                                 this->gameArray[coin->posX+1][coin->posY]=this->gameArray[coin->posX+1][coin->posY]==0?1:0;
                             }//周围右侧金币反转的条件
                             if(coin->posX-1>=0)
                             {
                                 coinBtn[coin->posX-1][coin->posY]->changeFlag();
                                 this->gameArray[coin->posX-1][coin->posY]=this->gameArray[coin->posX-1][coin->posY]==0?1:0;
                             }//周围左侧金币反转的条件
                             if(coin->posY+1<=3)
                             {
                                 coinBtn[coin->posX][coin->posY+1]->changeFlag();
                                 this->gameArray[coin->posX][coin->posY+1]=this->gameArray[coin->posX][coin->posY+1]==0?1:0;
                             }//周围上侧金币反转的条件
                             if(coin->posY-1>=0)
                             {
                                 coinBtn[coin->posX][coin->posY-1]->changeFlag();
                                 this->gameArray[coin->posX][coin->posY-1]=this->gameArray[coin->posX][coin->posY-1]==0?1:0;
                             }//周围下侧金币反转的条件
                             for(int i=0;i<4;i++)
                             {
                                 for (int j=0;j<4;j++)
                                 {
                                     this->coinBtn[i][j]->isWin=false;
                                 }
                             }//翻完所有金币后恢复所有按钮
                             this->isWin=true;
                             for(int i=0;i<4;i++)
                             {
                                 for (int j = 0; j < 4; j++)
                                 {
                                     if(coinBtn[i][j]->flag==false)//只要有一个是反面，即为失败
                                     {
                                         this->isWin=false;
                                         break;
                                     }
                                 }
                              }//判断是否胜利
                             if(this->isWin==true)
                             {
                                 qDebug()<<"游戏胜利";
                                 for(int i=0;i<4;i++)
                                 {
                                     for (int j = 0; j < 4; j++)
                                     {
                                         coinBtn[i][j]->isWin=true;
                                     }
                                 }//将所有按钮的胜利标志改为true
                                 QPropertyAnimation* animation=new QPropertyAnimation(winLabel,"geometry");//移动胜利图片
                                 animation->setDuration(1000);//设置时间间隔
                                 animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));//设置开始位置
                                 animation->setEndValue(QRect(winLabel->x(),winLabel->y()+120,winLabel->width(),winLabel->height()));//设置结束位置
                                 animation->setEasingCurve(QEasingCurve::OutBounce);//设置曲线
                                 animation->start();//执行动画
                             }
                         });//延迟周围金币的翻转
                     });
        }
    }
}
void PlayScene::paintEvent(QPaintEvent*)
    {
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/levelscene.png");//插入levelscene
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);
    pix.load(":/res/tittle.png");//插入tittle
    painter.drawPixmap(0, 0, pix);
}//重新paintEvent事件，画背景图

